package com.pyl.admin.system.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pyl.admin.core.enums.ResultEnum;
import com.pyl.admin.core.enums.StatusEnum;
import com.pyl.admin.core.exception.ResultException;
import com.pyl.admin.core.shiro.ShiroUtil;
import com.pyl.admin.core.web.PageUtil;
import com.pyl.admin.core.web.PageUtil.PageSort;
import com.pyl.admin.system.entity.SysDict;
import com.pyl.admin.system.mapper.SysDictMapper;
import com.pyl.admin.system.service.SysDictService;
import com.pyl.admin.system.validator.DictForm;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 灵魂贰婶
 * @since 2019-06-28
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

	@Autowired
	private SysDictMapper sysDictMapper;

	@Override
	public PageInfo<SysDict> selectDictListPage( Integer status, String query) {
		PageSort pageSort = PageUtil.pageRequest();
		PageHelper.startPage(pageSort.getCurrent(), pageSort.getSize());
		PageHelper.orderBy(pageSort.getOrderByColumn()+ " " + pageSort.getIsAsc());
		List<SysDict> list = sysDictMapper.selectDictListPage(status, query);
		PageInfo<SysDict> pageInfo = new PageInfo<SysDict>(list);
		return pageInfo;
	}

	@Override
	@Transactional
	public boolean saveOrUpdate(DictForm dictForm) {

		if (ObjectUtil.isNull(dictForm.getId())) { // 保存
			//判断是否存在
			if (ObjectUtil.isNotNull(sysDictMapper.selectOne(new SysDict().setName(dictForm.getName())))) {
				throw new ResultException(ResultEnum.DICT_EXIST);
			}
			SysDict sysDict = new SysDict();
			// 拷贝属性 忽略null
			BeanUtil.copyProperties(dictForm, sysDict, CopyOptions.create().setIgnoreNullValue(true));
			sysDict.setCreateBy(ShiroUtil.getSubject().getId());
			sysDict.setCreateDate(new Date());
			sysDict.setCreateByName(ShiroUtil.getSubject().getNickname());
			sysDict.setUpdateBy(ShiroUtil.getSubject().getId());
			sysDict.setUpdateByName(ShiroUtil.getSubject().getNickname());
			sysDict.setUpdateDate(new Date());
			sysDict.setStatus(Integer.valueOf(StatusEnum.OK.getCode()));
			return SqlHelper.retBool(sysDictMapper.insert(sysDict));
		}

		SysDict sysDict = sysDictMapper.selectById(dictForm.getId());
		BeanUtil.copyProperties(dictForm, sysDict, CopyOptions.create().setIgnoreNullValue(true));
		sysDict.setUpdateBy(ShiroUtil.getSubject().getId());
		sysDict.setUpdateByName(ShiroUtil.getSubject().getNickname());
		sysDict.setUpdateDate(new Date());

		return SqlHelper.retBool(sysDictMapper.updateById(sysDict));
	}

	@Override
	@Transactional
	public boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
		SysDict sysDict = new SysDict();
		sysDict.setStatus(Integer.valueOf(statusEnum.getCode()));
		return SqlHelper.retBool(sysDictMapper.update(sysDict, new EntityWrapper<SysDict>().in("id", idList)));
	}

}
